//
//  TKIMMsgView.m
//  TKIM
//
//  Created by liupm on 15/7/27.
//  Copyright (c) 2015年 liupm. All rights reserved.
//

#import "TKIMTxtMsgView.h"
#import "TKIMYYKitHelper.h"


@interface TKIMTxtMsgView()<TKIMYYKitLableDelegate>


@end

@implementation TKIMTxtMsgView

/**
 *  @Author lpm, 15-07-30 11:07:12
 *
 *  @brief  组装界面
 */
-(void)drawView
{
    [super drawView];
    [self.backgroundImageView addSubview:self.yyLabel];
}


//-(TKIMAttributedLabel *)messageLabel
//{
//    if (!_messageLabel) {
//        _messageLabel = [[TKIMAttributedLabel alloc] initWithFrame:CGRectZero];
//        _messageLabel.font = [UIFont systemFontOfSize:15.0f];
//        _messageLabel.textColor = [UIColor blackColor];
//        _messageLabel.numberOfLines = 0;
//        _messageLabel.lineBreakMode = NSLineBreakByCharWrapping;
//        _messageLabel.backgroundColor = [UIColor clearColor];
//        _messageLabel.userInteractionEnabled = YES;
//        _messageLabel.delegate = self;
//    }
//    return _messageLabel;
//}
-(YYLabel *)yyLabel
{
    if (_yyLabel == nil) {
        _yyLabel = [[YYLabel alloc] init];
        _yyLabel.userInteractionEnabled = YES;
        _yyLabel.numberOfLines = 0;
        UIFont *font = [UIFont systemFontOfSize:15];
        _yyLabel.font = font;
        _yyLabel.textColor = [UIColor blackColor];
        _yyLabel.lineBreakMode = NSLineBreakByCharWrapping;
        _yyLabel.displaysAsynchronously = YES; /// enable async display
        _yyLabel.ignoreCommonProperties = YES;
        _yyLabel.textVerticalAlignment = YYTextVerticalAlignmentTop;
        [_yyLabel setBackgroundColor:[UIColor clearColor]];

    }
    return _yyLabel;
}


/**
 *  @Author lpm, 15-07-27 17:07:14
 *
 *  @brief  设置文字
 *
 *  @param text
 */
-(void)setMessage:(TKIMMessage *)message
{
    [super setMessage:message];
    UIColor * color;
    if(message.isSender)
    {
        color = [UIColor whiteColor];
    }
    else
    {
        color = [UIColor blackColor];
    }

    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:4];//调整行间距
    [paragraphStyle setParagraphSpacing:4];//调整行间距
    [paragraphStyle setLineBreakMode:NSLineBreakByCharWrapping];
  NSDictionary*  attri = [NSDictionary dictionaryWithObjects:@[[UIFont systemFontOfSize:15],color,paragraphStyle] forKeys:@[NSFontAttributeName,NSForegroundColorAttributeName,NSParagraphStyleAttributeName]];
    
    NSMutableAttributedString* stratt2=[[TKIMYYKitHelper shareInstance] processCommentContent:message.text style:attri textColor:color];
    [TKIMYYKitHelper shareInstance].delegate=self;
    
    
   
    
    YYTextContainer *container2 = [YYTextContainer containerWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width*0.65, MAXFLOAT)];
    YYTextLayout *textLayout2 = [YYTextLayout layoutWithContainer:container2 text:stratt2];
    
    self.yyLabel.textLayout = textLayout2;
    self.yyLabel.size = textLayout2.textBoundingSize;
    CGRect msgRect = [self getMsgFrame:message size:textLayout2.textBoundingSize];
    self.yyLabel.frame = msgRect;
}




/**
 *  @Author lpm, 15-11-06 11:11:05
 *
 *  @brief  行高
 *
 *  @param msg
 *
 *  @return
 */
+(CGFloat)heightForRow:(TKIMMessage *)msg
{

    
    NSMutableAttributedString* stratt2=[[TKIMYYKitHelper shareInstance] processCommentContent:msg.text style:nil textColor:[UIColor clearColor]];
    YYTextContainer *container2 = [YYTextContainer containerWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width*0.65, MAXFLOAT)];
    YYTextLayout *textLayout2 = [YYTextLayout layoutWithContainer:container2 text:stratt2];
    return textLayout2.textBoundingSize.height + TKIMChatMsgPadding + 5;

}


/**
 *  @Author lpm, 16-03-16 14:03:15
 *
 *  @brief  链接点击事件
 *
 *  @param linkData
 */
- (void)clickedOnLink:(id)linkData
{
    [self routerEventWithName:TKIMLinkOnClick userInfo:linkData];
}

-(void)clickedOnLink:(id)linkData clickType:(TKIMTextClickType)type{

    if (type == TKIMTextClickType_Url) {//打开Url
        [self routerEventWithName:TKIMLinkOnClick userInfo:linkData];
    }else{//直接拨打电话
//        NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",linkData];

    
    }
    
}

@end
